package reflect;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URISyntaxException;

public class Test3 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, URISyntaxException {
        File dir = new File(Test3.class.getResource(".").toURI());
        if (dir.isDirectory()) {
            File[] subs = dir.listFiles(file -> file.getName().endsWith(".class"));
            System.out.println("当前目录下有" + subs.length + "个class文件");
            Class pname = Test3.class;
            String packageName = pname.getPackage().getName();
            for (File sub : subs) {
                String[] str = sub.getName().split("\\.");
                Class cls = Class.forName(packageName + "." + str[0]);
                Constructor con = cls.getDeclaredConstructor();
                if(con.getModifiers()!=Modifier.PUBLIC){
                    continue;
                }
                Object o = con.newInstance();
                Method[] methods = cls.getDeclaredMethods();
//                System.out.println(methods.length);   //每个类的方法个数
                for (int i = 0; i < methods.length; i++) {
                    if (methods[i].getParameterCount() == 0 && methods[i].getModifiers() == Modifier.PUBLIC
                            && methods[i].getName().contains("s")) {
                        System.out.println(methods[i].getName());
                        Method method = cls.getDeclaredMethod(methods[i].getName());
                        method.invoke(o);
                    }
                }
            }
            {
            }
        }
    }
}
